package com.lf.controller;

import com.lf.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Reference
    private UserService userService;

    // 注册接⼝，true成功，false失败
    @GetMapping("/register/{email}/{password}/{code}")
    public Boolean register(@PathVariable("email") String email, @PathVariable("password") String password,@PathVariable("code") String code) {
        return userService.register(email,password,code);
    }

    // 是否已注册，根据邮箱判断,true代表已经注册过，false代表尚未注册
    @GetMapping("/isRegistered/{email}")
    public Boolean isRegistered(@PathVariable("email") String email) {
        return userService.isRegistered(email);
    }

    // 登录接⼝，验证⽤户名密码合法性，根据⽤户名和密码⽣成token，token存⼊数据库，并写⼊cookie中，登录成功返回true，否则返回false
    @GetMapping("/login/{email}/{password}")
    public Boolean login(@PathVariable("email") String email,@PathVariable("password") String password, HttpServletResponse response){
        return userService.login(email,password,response);
    }

    // 根据token查询⽤户登录邮箱接⼝
    @GetMapping("/info/{token}")
    public String getEmailByToken(@PathVariable("token") String token) {
        System.out.println("token is : " + token);
        String email = userService.getEmailByToken(token);
        System.out.println("email is : " + email);
        return email;
    }


}
